package com.zenithmind.news.websocket;

import org.springframework.web.socket.WebSocketSession;

/**
 * WebSocket消息处理器接口 - 遵循开闭原则
 * 支持扩展新的消息类型而无需修改现有代码
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
public interface WebSocketMessageHandler {

    /**
     * 获取支持的消息类型
     */
    String getSupportedMessageType();

    /**
     * 处理消息
     * 
     * @param session WebSocket会话
     * @param request 请求对象
     * @param sessionInfo 会话信息
     * @return 处理结果
     */
    WebSocketResponse handleMessage(WebSocketSession session, WebSocketRequest request, 
                                   WebSocketSessionManager.UserSessionInfo sessionInfo);

    /**
     * WebSocket请求
     */
    class WebSocketRequest {
        private String type;
        private Object data;

        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        
        public Object getData() { return data; }
        public void setData(Object data) { this.data = data; }
    }

    /**
     * WebSocket响应
     */
    class WebSocketResponse {
        private String type;
        private Boolean success;
        private String message;
        private Object data;
        private java.time.LocalDateTime timestamp;

        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        
        public Boolean getSuccess() { return success; }
        public void setSuccess(Boolean success) { this.success = success; }
        
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        
        public Object getData() { return data; }
        public void setData(Object data) { this.data = data; }
        
        public java.time.LocalDateTime getTimestamp() { return timestamp; }
        public void setTimestamp(java.time.LocalDateTime timestamp) { this.timestamp = timestamp; }

        public static WebSocketResponse success(String type, Object data) {
            WebSocketResponse response = new WebSocketResponse();
            response.setType(type);
            response.setSuccess(true);
            response.setData(data);
            response.setTimestamp(java.time.LocalDateTime.now());
            return response;
        }

        public static WebSocketResponse error(String type, String message) {
            WebSocketResponse response = new WebSocketResponse();
            response.setType(type);
            response.setSuccess(false);
            response.setMessage(message);
            response.setTimestamp(java.time.LocalDateTime.now());
            return response;
        }

        public static WebSocketResponse system(String type, String message) {
            WebSocketResponse response = new WebSocketResponse();
            response.setType(type);
            response.setMessage(message);
            response.setTimestamp(java.time.LocalDateTime.now());
            return response;
        }
    }
}
